package cn.ld.fj.service.food;

import cn.ld.fj.dao.food.FoodDao;
import cn.ld.fj.dao.food.FoodTypeDao;
import cn.ld.fj.entity.Food;
import cn.ld.fj.entity.FoodTaste;
import cn.ld.fj.entity.FoodType;
import net.esoar.modules.orm.Page;
import net.esoar.modules.orm.PropertyFilter;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 安全相关实体的管理类, 包括用户,角色,资源与授权类.
 *
 * @author fly
 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class FoodManager {

    private static Logger logger = LoggerFactory.getLogger(FoodManager.class);
    @Autowired
    private FoodDao foodDao;


    //-- User Manager --//
    @Transactional(readOnly = true)
    public Food getOne(Long id) {
        return foodDao.get(id);
    }

    public void save(Food entity) {
        foodDao.save(entity);
    }


    public Page<Food> getPage(Page<Food> page, List<PropertyFilter> filters) {
        return foodDao.findPage(page, filters);
    }

    public void delete(Long id) {
        foodDao.delete(id);
    }

    @Transactional(readOnly = true)
    public boolean getFood(Long id) {
        List<Food> foods = foodDao.findBy("foodType", id);
        if (CollectionUtils.isNotEmpty(foods)) {
            return true;
        } else {
            return false;
        }

    }
}
